package com.mdd.admin.controller.order;

import com.alipay.api.AlipayApiException;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.config.aop.Log;
import com.mdd.admin.service.order.IOrderService;
import com.mdd.admin.service.salesman.ISalesmanService;
import com.mdd.admin.validate.order.OrderPageParam;
import com.mdd.admin.vo.finance.FinanceVo;
import com.mdd.admin.vo.order.OrderDetailVo;
import com.mdd.admin.vo.order.OrderListVo;
import com.mdd.admin.vo.order.StatisticsOrderVo;
import com.mdd.common.core.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

/**
 * 订单管理
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("api/order")
public class OrderController {

    @Resource
    IOrderService iOrderService;

    @Resource
    private ISalesmanService iSalesmanService;

    /**
     * 订单列表
     *
     * @param params 搜索参数
     * @return Object
     */
    @ApiOperation(value = "订单管理 - 列表")
    @PostMapping("/list")
    public PageResult<OrderListVo> list(@Validated @RequestBody OrderPageParam params) {
        return iOrderService.list(params, false);
    }

    /**
     * 订单详情
     *
     * @param id 主键ID
     * @return Object
     */
    @ApiOperation(value = "订单管理 - 订单详情")
    @GetMapping("/detail")
    public OrderDetailVo detail(@Validated @RequestParam("id") Long id) {
        return iOrderService.detail(id);
    }

    /**
     * @return java.lang.Object
     * @Author slien
     * @Description 统计订单状态的数据
     * @Date 11:47 2022/10/11
     * @Param []
     **/
    @ApiOperation(value = "订单管理 - 统计订单状态的数据")
    @GetMapping("/statistics")
    public StatisticsOrderVo statistics(@RequestParam(value = "isSalesman", required = false) Integer isSalesman) {
        Integer userId = 0;
        if(Objects.nonNull(isSalesman) && isSalesman == 1) {
            userId = LikeAdminThreadLocal.getAdminId();
        }
        return iOrderService.statistics(userId);
    }

    /**
     * @return java.lang.Object
     * @Author slien
     * @Description 商家备注
     * @Date 11:47 2022/10/11
     * @Param []
     **/
    @ApiOperation(value = "订单管理 - 商家备注")
    @GetMapping("/remark")
    public void remark(@RequestParam("id") Long id, @RequestParam(value = "remark", required = false) String remark) {
        Integer userId = LikeAdminThreadLocal.getAdminId();
        iOrderService.remark(id, userId, remark);
    }

    /**
     * @return java.lang.Object
     * @Author slien
     * @Description 后台取消订单
     * @Date 13:57 2022/10/11
     * @Param [id]
     **/
    @ApiOperation(value = "订单管理 - 后台取消订单")
    @GetMapping("/cancel")
    public void cancel(@RequestParam("id") Long id) throws WxPayException, AlipayApiException {
        Integer userId = LikeAdminThreadLocal.getAdminId();
        iOrderService.cancelOrder(id, userId);
    }

    /**
     * @return java.lang.Object
     * @Author slien
     * @Description 后台指派订单给师傅
     * @Date 14:30 2022/10/11
     * @Param [id, staffId]
     **/
    @ApiOperation(value = "订单管理 - 指派订单给师傅")
    @GetMapping("/dispatch")
    public void dispatch(@RequestParam("id") Long id, @RequestParam("staffId") String[] staffId) {
        Integer userId = LikeAdminThreadLocal.getAdminId();
        Integer[] array = Arrays.stream(staffId).map(Integer::parseInt).toArray(Integer[]::new);
        iOrderService.dispatchStaff(id, array, userId);
    }

    /**
     * @return java.lang.Object
     * @Author slien
     * @Description 核销订单
     * @Date 14:43 2022/10/11
     * @Param [id, code]
     **/
    @ApiOperation(value = "订单管理 - 核销订单")
    @GetMapping("/verification")
    public void verification(@RequestParam("id") Long id, @RequestParam("code") String code) {
        Integer userId = LikeAdminThreadLocal.getAdminId();
        iOrderService.verificationOrder(id, code, userId);
    }

    @ApiOperation("绑定包年保姆")
    @PostMapping("/bindAnnualService")
    public void bindAnnualService(@RequestBody Map<String, String> params){
        iOrderService.bindAnnualService(params);
    }


    /**
     * 订单删除
     *
     * @return Object
     */
    @ApiOperation(value = "订单管理 - 订单删除")
    @Log(title = "订单删除")
    @GetMapping("/del")
    public void del(@RequestParam("id") Long id) {
        iOrderService.del(id);
    }

    @ApiOperation(value = "推销员 - 订单查询")
    @Log(title = "推销员 - 订单查询")
    @PostMapping("/referrersOrderList")
    public PageResult<OrderListVo> referrersOrderList(@RequestBody OrderPageParam params) {
        return iOrderService.salesmanList(params);
    }

    @Log(title = "推销员 - 推销员订单金额统计")
    @ApiOperation(value = "推销员 - 推销员订单金额统计")
    @PostMapping("/salesmanStatistics")
    public FinanceVo salesmanStatistics(@RequestBody OrderPageParam params) {
        return iOrderService.countList(params);
    }

}
